package com.legal.service.chat.impl;

import com.legal.domain.vo.MessageVO;
import com.legal.service.chat.ChatMessageService;
import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
@RequiredArgsConstructor
@Service
public class ChatMessageServiceImpl implements ChatMessageService {
    private final ChatMemory chatMemory;
    @Override
    public List<MessageVO> getChatHistory(String chatId) {
        List<Message> messages = chatMemory.get(chatId,Integer.MAX_VALUE);
        if(messages == null){
            return List.of();
        }
        //return messages.stream().map(message -> new MessageVO(message)).toList();
        List<MessageVO> list = messages
                .stream()
                .map(MessageVO::new)
                .collect(Collectors.toList()); //返回 ArrayList（可变集合）
        Collections.reverse(list); // 反转（存储是leftpush，但是前端展示的是右边开始的）
        return list;
    }
}
